//
//  InfoVC.m
//  iphone golf range assistant
//
//  Created by S W Consultancy on 12/09/2011.
//  Copyright 2011 SW Consultancy. All rights reserved.
//

#import "MainVC.h"
#import "InfoVC.h"
#import "RwData.h"

@implementation InfoVC
@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)doneButtonClicked {
    [delegate infoDone];
}

#pragma mark - View lifecycle

-(UILabel*)addLabel :(NSString*)text :(int)w :(int)h :(float)x :(float)y{
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.text = text;
    label.textAlignment = UITextAlignmentCenter;
    label.bounds = CGRectMake(0, 0, w, h);
    label.center = CGPointMake(x, y);
    //label.backgroundColor = [UIColor blueColor];
    //label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin;
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin;
    [infoView addSubview:label];
    return label;
}

-(UIButton*)addButton :(NSString*)text :(int)w :(int)h :(float)x :(float)y :(SEL)selector{ 
    UIButton*  button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.bounds = CGRectMake(0, 0, w, h);
    button.center = CGPointMake(x, y);
    //button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin;
    button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin;
    [button setTitle:text forState:UIControlStateNormal];
    [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    [infoView addSubview:button];
    return button;
}


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
    NSLog(@"loadView Info");
    rwClubs = [RwData singleton:@"Clubs"];
    rwRates = [RwData singleton:@"Rates"];

    CGRect rect=[UIScreen mainScreen].applicationFrame;
    CGSize size=rect.size;

    infoView=[[UIScrollView alloc] initWithFrame:rect];
    infoView.contentSize=CGSizeMake(size.width ,2*size.height);
    self.view = infoView;
    
    [self addButton:@"Done"               :96   :32 :2*size.width/4 :1*size.height/18 :@selector(doneButtonClicked)];
    [self addLabel :@"How are you doing?" :96*2 :32 :2*size.width/4 :2*size.height/16];

    int n,bign;
    
    n=3;
    for (NSString* key in [[rwClubs.dict allKeys] sortedArrayUsingSelector:@selector(compare:)]){
        [self addLabel :key :size.width/4 :32 :1*size.width/8 :n*size.height/16];
        [self addLabel :[rwClubs read:key] :size.width/4 :32 :3*size.width/8 :n*size.height/16];
        n++;
    }
    bign=n;
    n=3;
    for (NSString* key in [[rwRates.dict allKeys] sortedArrayUsingSelector:@selector(compare:)]){
        [self addLabel :key :size.width/4 :32 :5*size.width/8 :n*size.height/16];
        [self addLabel :[rwRates read:key] :size.width/4 :32 :7*size.width/8 :n*size.height/16];
        n++;
    }
    bign = MAX(n, bign);
    [self addButton:@"Done"               :96   :32 :2*size.width/4 :bign*size.height/16 :@selector(doneButtonClicked)];
}


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES; //(interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
